package lockstrategy;

import java.util.concurrent.Semaphore;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 74646
 * Date: 2022-05-28
 * Time: 23:58
 */
public class Demo5 {
    public static void main(String[] args) {
        // 初始化的值表示可用资源的个数
        Semaphore semaphore = new Semaphore(4);
        try {
            //申请资源( P 操作)
            //如果申请的资源个数 > 可用资源的个数，就会阻塞等待
            semaphore.acquire();
            System.out.println("申请成功");
            semaphore.acquire();
            System.out.println("申请成功");
            semaphore.acquire();
            System.out.println("申请成功");
            semaphore.acquire();
            System.out.println("申请成功");
            semaphore.acquire();
            System.out.println("申请成功");
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
